package com.demo.netty.demo01;

import com.demo.netty.demo01.handler.CatClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

import java.net.InetSocketAddress;

public class CatClientDemo {


    public static void main(String[] args) throws InterruptedException {

        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup worker = new NioEventLoopGroup();
        bootstrap.group(worker)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new CatClientHandler());
                    }
                });

        ChannelFuture future = bootstrap
                .connect(new InetSocketAddress("127.0.0.1", 9999))
                .sync();
        future.addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                if (future.isSuccess()) {
                    System.out.println("---------链接服务端成功！---------------");
                } else if (!future.isSuccess()) {
                    System.out.println("---------链接服务端失败！---------------！");
                }
            }
        });
        future.channel().closeFuture().sync();

        worker.shutdownGracefully();


    }
}
